package cn.jzq.demo.web;


import cn.hutool.core.date.DateUtil;
import cn.jzq.demo.entity.GeneralStudent;
import cn.jzq.demo.form.StudentForm;
import cn.jzq.demo.service.IGeneralStudentService;
import cn.jzq.demo.util.R;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jzq
 * @since 2018-05-22
 */
@RestController
@RequestMapping("/student")
public class GeneralStudentController {

    @Autowired
    private IGeneralStudentService studentService;

    @ApiOperation(value="创建学生", notes="根据GeneralStudent对象创建用户")
    @ApiImplicitParam(name = "studentForm", value = "学生详细实体student", required = true, dataType = "StudentForm")
    @PostMapping("/")
    public R postStudent(@ModelAttribute StudentForm studentForm) {
        GeneralStudent student = new GeneralStudent(studentForm);
        Date now = DateUtil.date();
        student.setCreateTime(now);
        student.setUpdateTime(now);

        studentService.insertStudent(student);

        return R.ok();
    }


    @ApiOperation(value="获取学生列表", notes="stu list")
    @GetMapping("/")
    public R student() {
        return R.ok().put("data", studentService.listStudent());
    }

    @ApiOperation(value="获取学生列表", notes="stu list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "pageNum", paramType = "path", dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", paramType = "path", dataType = "Long")
    })
    @GetMapping("/page/{pageNum}/{pageSize}")
    public R student(@PathVariable Long pageNum, @PathVariable Long pageSize) {
        return R.ok().put("data", studentService.listStudentPage(pageNum, pageSize));
    }


    @ApiOperation(value = "根据stuNo获取学生")
    @ApiImplicitParam(name = "stuNo", value = "stuNo", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{stuNo}")
    public R getStudent(@PathVariable String stuNo) {
        return R.ok().put("data", studentService.getStudentByStuNo(stuNo));
    }

    @ApiOperation(value = "根据stuNo获取学生Test")
    @ApiImplicitParam(name = "stuNo", value = "stuNo", required = true, dataType = "String", paramType = "path")
    @GetMapping("/test/{stuNo}")
    public R getStudentTest(@PathVariable String stuNo) {
        return R.ok().put("data", studentService.getStudentByStuNo(stuNo));
    }


    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "path"),
        @ApiImplicitParam(name = "StudentForm", value = "学生表单", required = true, dataType = "StudentForm")
    })
    @PutMapping("/{id}")
    public R putStudent(@PathVariable Integer id, @ModelAttribute StudentForm studentForm) {
        Integer id2 = Integer.valueOf(id);
        studentService.updateStudent(id2, studentForm);
        return R.ok();
    }


    @PostMapping("/batch")
    public R saveBatch() {
        studentService.saveBatch();
        return R.ok();
    }
}
